package com.yanqu.road.server.command;

import com.yanqu.road.NettyMgr;
import com.yanqu.road.define.YanQuNettyDefine;
import com.yanqu.road.connect.YanQuConnection;
import com.yanqu.road.pb.server.LoadProto;
import com.yanqu.road.connect.ServerConnectionGroup;
import com.yanqu.road.connect.ServerLinkControl;
import com.yanqu.road.message.YanQuMessage;
import io.netty.channel.ChannelHandlerContext;

public abstract class AbsServerConnect implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        LoadProto.LoadMsg registerMsg = LoadProto.LoadMsg.parseFrom(yanQuMessage.toByteArray());
        ServerConnectionGroup serverSessionGroup = ServerLinkControl.addServerSessionGroup(registerMsg.getServerIndex(),
                new YanQuConnection(ctx.channel(), true, 1024, registerMsg.getLinkIndex()),
                YanQuNettyDefine.LINK_COUNT_MAX, registerMsg.getLinkIndex());
        // 设置client标记
        ctx.channel().attr(NettyMgr.LINKED_CLIENT).set(serverSessionGroup);
        // 调用上层overwrite
        execute(serverSessionGroup);
    }

    public void execute(ServerConnectionGroup serverSessionGroup) throws Exception {
        // 预留
    }

}
